package com.ziqikeji.xianyan.service.article;

import com.ziqikeji.xianyan.common.BaseService;
import com.ziqikeji.xianyan.entity.article.Article;
import com.ziqikeji.xianyan.enumeration.DeleteFlagEnum;
import com.ziqikeji.xianyan.enumeration.YesOrNoEnum;
import com.ziqikeji.xianyan.repository.article.ArticleDao;
import org.hibernate.mapping.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Query;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

/**
 * 文章（帖子）service
 * @author liuyu
 */
@Service
public class ArticleService extends BaseService<Article, ArticleDao> {

    @Autowired
    private ArticleDao articleDao;
    /**
     * 保存发帖
     *
     * @param title   标题
     * @param content 内容
     * @return article
     */
    public Article saveArticle(String title, String content) {
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        article.setWitter(user.getUsername());
        article.setIsPassed(YesOrNoEnum.NO);
        return super.save(article);
    }
    @Override
    public Page<Article> getPage(int pageNumber, int pageSize) {
        Pageable pageable = PageRequest.of(pageNumber, pageSize, new Sort(Sort.Direction.ASC, "createTime"));
        return this.articleDao.queryByDeleteFlagEnumAndIsPassed(DeleteFlagEnum.NO_DELETE, YesOrNoEnum.NO, pageable);
    }


    public Page<Article> getPageYes(int pageNumber, int pageSize) {
        Pageable pageable = PageRequest.of(pageNumber, pageSize, new Sort(Sort.Direction.ASC, "createTime"));
        return this.articleDao.queryByDeleteFlagEnumAndIsPassed(DeleteFlagEnum.NO_DELETE, YesOrNoEnum.YES, pageable);
    }

    /*查询文章表中id不等于推荐文章表中的文章id的文章*/


}
